#coding=utf-8
import requests
from lxml import html as H
from collections import defaultdict

urls = ['http://10.141.209.138:8090/display/VUL/Android+Security+Bulletin+July+2017',
 'http://10.141.209.138:8090/display/VUL/Android+Security+Bulletin+June+2017',
 'http://10.141.209.138:8090/display/VUL/Android+Security+Bulletin+May+2017',
 'http://10.141.209.138:8090/display/VUL/Android+Security+Bulletin+April+2017',
 'http://10.141.209.138:8090/display/VUL/Android+Security+Bulletin+March+2017',
 'http://10.141.209.138:8090/display/VUL/Android+Security+Bulletin+February+2017',
 'http://10.141.209.138:8090/display/VUL/Android+Security+Bulletin+January+2017']

cookies =  {'JSESSIONID': 'D8279155F4542C2928B49951F4CF190F'}
# 此处的Cookie每天需要更新


def debug(x):
    pass
    # print x

def CollectCveByModule(moduleName):
    CVE_list = []

    for url in urls:
        debug(url)
        text = requests.get(url, cookies=cookies).text
        t = H.fromstring(text)
        trs = t.xpath("""//div[@id="main-content"]//tr""")
        debug(trs)
        if not trs:
            continue
        heads = [x.text_content().strip() for x in trs[0].getchildren()]

        for tr in trs[1:]:
            tds = tr.getchildren()
            if len(tds)==len(heads):
                mod = tds[heads.index("Module")].text_content().strip().lower().replace("frameworks","framework").replace("externel","external")
                if mod == moduleName:
                    # import IPython; IPython.embed()
                    CVE_list.append(
                        (tds[heads.index("Android Security Bulletin CVE number")].text_content(),
                        tds[heads.index("Patch")].getchildren()[0].attrib['href']
                        ))

        debug(CVE_list)
        debug("\n\n\n")
    
    print len(CVE_list)
    for x in CVE_list:
        print x
    

def ModulesStat():
    modules = defaultdict(int)


    for url in urls:
        debug(url)
        text = requests.get(url, cookies=cookies).text
        t = H.fromstring(text)
        # trs = t.xpath("""//*[@id="main-content"]/div/table/tbody/tr""")
        trs = t.xpath("""//div[@id="main-content"]//tr""")
        # debug(t.xpath("""//div[@id="main-content"]"""))
        debug(trs)
        if not trs:
            continue
        for k,v in enumerate(trs[0].getchildren()):
            if v.text_content().strip()=="Module":
                index = k
        debug(index)
        for tr in trs:
            tds = tr.getchildren()
            if len(tds)>index:
                mod = tds[index].text_content().strip()
                if mod:
                    mod = mod.lower().replace("frameworks","framework").replace("externel","external")
                    modules[mod] += 1

        debug(modules)
        debug("\n\n\n")


    for k,v in sorted(modules.iteritems(), key=lambda x:x[1], reverse=True):
        print k,",",v

# CollectCveByModule("external/libavc")
ModulesStat()